package com.flower.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.flower.domain.excel.EncyclopediaExcelData;
import com.flower.domain.excel.EncyclopediaImportListener;
import com.flower.service.EncyclopediaExcelService;
import com.flower.service.FlowerEncyclopediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 花卉百科Excel服务实现类
 */
@Slf4j
@Service
public class EncyclopediaExcelServiceImpl implements EncyclopediaExcelService {
    @Autowired
    private EncyclopediaImportListener importListener;

    @Override
    public void downloadTemplate(OutputStream outputStream) {
        try {
            // 创建示例数据
            List<EncyclopediaExcelData> exampleData = new ArrayList<>();
            EncyclopediaExcelData example = new EncyclopediaExcelData();
            example.setName("蝴蝶兰");
            example.setScientificName("Phalaenopsis");
            example.setFamily("兰科");
            example.setGenus("蝴蝶兰属");
            example.setAlias("蝶兰、蝴蝶花");
            example.setOrigin("东南亚热带地区");
            example.setRightPlace("北京、上海、郑州");
            example.setCultivationDifficulty(3); // 中等难度
            example.setIntroduction("蝴蝶兰是兰科蝴蝶兰属附生植物，因花形似蝴蝶翩翩起舞而得名。");
            example.setFeatures("株型优美，花色丰富，花期长，是著名的室内观赏花卉。");
            example.setLightRequirement("散射光");
            example.setWaterRequirement("保持湿润");
            example.setSoilType("疏松透气");
            example.setTemperatureMin(18.0);
            example.setTemperatureMax(28.0);
            example.setHumidity("60%-80%");
            example.setFertilization("每月一次");
            exampleData.add(example);

            // 设置Excel样式
            LongestMatchColumnWidthStyleStrategy columnWidthStyleStrategy = new LongestMatchColumnWidthStyleStrategy();

            // 写入Excel
            EasyExcel.write(outputStream, EncyclopediaExcelData.class)
                    .registerWriteHandler(columnWidthStyleStrategy)
                    .sheet("花卉百科导入模板")
                    .doWrite(exampleData);
        } catch (Exception e) {
            log.error("生成Excel模板失败", e);
            throw new RuntimeException("生成Excel模板失败");
        }
    }

    @Override
    public int importEncyclopediaData(MultipartFile file) throws IOException {
        try {
            // 重置导入计数器
            importListener.resetCount();
            
            // 读取Excel文件
            EasyExcel.read(file.getInputStream(), EncyclopediaExcelData.class, importListener)
                    .sheet()
                    .doRead();
            
            // 返回成功导入的数量
            return importListener.getSuccessCount();
        } catch (Exception e) {
            log.error("导入数据失败", e);
            throw new RuntimeException("导入数据失败：" + e.getMessage());
        }
    }
} 